<?php
/**
 * Created by PhpStorm.
 * User: HeiYanHeiMao
 * Date: 2020/2/27
 * Time: 17:44
 */

namespace app\controller;


use app\BaseController;
use app\middleware\Check;
use app\model\GoodsModel;
use app\Request;
use think\facade\Validate;
use think\validate\ValidateRule;

/**商品管理
 * Class Goods
 * @package app\controller
 */
class Goods extends BaseController
{
    protected $middleware = [Check::class];

    /**商户添加商品
     * @param Request $request
     * @return \think\response\Json
     */
    public function add(Request $request)
    {
        if (!$request->isPost()) return jsonFail('非法请求方式');
        // 接收参数
        $input['cate_id'] = $request->post('cate_id'); // 商品分类id
        $input['name'] = $request->post('name'); // 商品名称
        $input['pic'] = $request->post('pic'); // 商品图片
        $input['price'] = $request->post('price'); // 商品单价
        $input['detail'] = $request->post('detail'); // 描述
        //验证参数
        $validate = Validate::rule([
            'cate_id' => ValidateRule::isRequire(null, '缺少参数'),
            'name' => ValidateRule::isRequire(null, '缺少参数'),
            'pic' => ValidateRule::isRequire(null, '缺少参数'),
            'price' => ValidateRule::isRequire(null, '缺少参数'),
            'detail' => ValidateRule::isRequire(null, '缺少参数'),
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return GoodsModel::add($input, $request->param('token'));
    }
}